package cn.onesdream.app.api.controller;

import cn.onesdream.app.api.service.StarService;
import cn.onesdream.app.common.exception.ServiceException;
import cn.onesdream.app.common.util.ResponseResult;
import cn.onesdream.app.dao.entity.Star;
import cn.onesdream.app.dao.entity.StarType;
import cn.onesdream.app.dao.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author WangShiyu
 * @date 2021/3/9 13:42
 */
@RestController
@Api(tags = "小星星相关接口")
@RequestMapping("/api/app/star")
@Validated
public class StarController {
    @Autowired
    private StarService starService;

//    @ApiOperation("获取事件类型")
//    @GetMapping("/starType/{starTypeId}")
//    public ResponseResult<StarType> getStarType(@PathVariable("starTypeId") @NotNull Byte starTypeId){
//        StarType starType = starService.getStarType(starTypeId);
//        return new ResponseResult<>(starType);
//    }.

    @ApiOperation("获取小星星事件")
    @GetMapping("/getStar")
    public ResponseResult<List> getStar(){
        Integer userId = ((User) SecurityContextHolder.getContextHolderStrategy().getContext().getAuthentication().getPrincipal()).getId();
        List<HashMap<String, Object>> listStar = new ArrayList<>();
        List<Star> star = starService.getStar(userId);
        for (Star value : star) {
            HashMap<String, Object> starRecord = new HashMap<>();
            StarType starType = starService.getStarType(value.getStarId());
            starRecord.put("name", starType.getName());
            starRecord.put("number", starType.getNumber());
            starRecord.put("time", value.getTime());
            listStar.add(starRecord);
        }
        return new ResponseResult<>(listStar);
    }

    @ApiOperation("调用小星星事件")
    @PostMapping("/")
    public ResponseResult<Star> postStar(Integer userId, Byte starTypeId) throws ServiceException {
        if(starTypeId == 1 || starTypeId == 5){
            starService.addDailyStar(userId,starTypeId);
        }
        else{
            starService.addStar(userId,starTypeId);
        }
        return new ResponseResult<>();
    }

}
